package com.peternguyen.totolistpro;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Calendar;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RatingBar;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TimePicker;

import com.peternguyen.totolistpro.dao.*;
import com.peternguyen.totolistpro.data.Category;
import com.peternguyen.totolistpro.data.ToDo;

public class EditTodo extends Activity implements OnClickListener {
	TextView dtpDueDate;
	TextView dtpDueTime;
	EditText txtName;
	EditText txteMail;
	EditText txtNote;
	RatingBar rtbPriority;
	Calendar myCalendar;
	Spinner spnCat;
	ImageButton btnSave;
	ImageButton btnDelete;
	ImageButton btnTodoBack;
	CategoryDAO catDao;
	TodoDAO todoDao;
	Date deadlineDate;
	ToDo todo;
	SimpleDateFormat formatDay;
	SimpleDateFormat formatTime;
	

	@SuppressLint("SimpleDateFormat")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.edit_todo);
		
		myCalendar = Calendar.getInstance();
		deadlineDate = new Date();
		
		dtpDueDate = (TextView) findViewById(R.id.Date);
		dtpDueTime = (TextView) findViewById(R.id.Time);
		btnSave = (ImageButton) findViewById(R.id.btnSave);
		btnDelete = (ImageButton) findViewById(R.id.btnDel);
		btnTodoBack = (ImageButton) findViewById(R.id.btn_todo_back);
		txtName = (EditText) findViewById(R.id.txtName);
		txteMail = (EditText) findViewById(R.id.txtMail);
		txtNote = (EditText) findViewById(R.id.txtNote);
		rtbPriority = (RatingBar) findViewById(R.id.rtbPriority);
		
		dtpDueDate.setOnClickListener(this);
		dtpDueTime.setOnClickListener(this);
		btnSave.setOnClickListener(this);
		btnDelete.setOnClickListener(this);
		btnTodoBack.setOnClickListener(this);
		spnCat = (Spinner) findViewById(R.id.spnnewDate);
		//Get List Category
		catDao = new CategoryDAO(this);		
		List<Category> lstCat = catDao.getList();
		ArrayAdapter<Category> adapter = new ArrayAdapter<Category>(this,
				android.R.layout.simple_spinner_item,lstCat);
		spnCat.setAdapter(adapter);		
		
		Intent callerIntent = getIntent();
		Bundle bunle = callerIntent.getBundleExtra("Data");
		if(bunle != null){
			todo = (ToDo) bunle.getSerializable("ToDo");
			if(todo != null){
				txtName.setText(todo.get_name());
				Date duedline = todo.get_deadline_date();
				if(duedline != null){
					deadlineDate = duedline;					
					myCalendar.setTime(duedline);
					rtbPriority.setRating(todo.get_priority());
				}				
				txteMail.setText(todo.get_email());
				txtNote.setText(todo.get_notes());
				
				for (int i = 0;i < lstCat.size();i++) {
					if(lstCat.get(i).get_id() == todo.get_catid()){
						spnCat.setSelection(i);
						break;
					}
				}
			}
		}
		formatDay = new SimpleDateFormat("dd-MM-yyyy");
		formatTime = new SimpleDateFormat("HH:mm");	
		dtpDueDate.setText(formatDay.format(deadlineDate));
		dtpDueTime.setText(formatTime.format(deadlineDate));
		
	}

	DatePickerDialog.OnDateSetListener datePicker = new DatePickerDialog.OnDateSetListener() {
		@SuppressWarnings("deprecation")
		@Override
		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {
			myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
			myCalendar.set(Calendar.MONTH, monthOfYear);
			myCalendar.set(Calendar.YEAR, year);
			
			Date date = myCalendar.getTime();
			
			dtpDueDate.setText(formatDay.format(date));			
			
			deadlineDate.setYear(year-1900);
			deadlineDate.setMonth(monthOfYear);
			deadlineDate.setDate(dayOfMonth);
				
		}
	};

	TimePickerDialog.OnTimeSetListener timePicker = new TimePickerDialog.OnTimeSetListener() {
		@SuppressWarnings("deprecation")
		@Override
		public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
			dtpDueTime.setText(hourOfDay + " : " + minute);
			deadlineDate.setHours(hourOfDay);
			deadlineDate.setMinutes(minute);
		}
	};

	@Override
	public void onClick(View v) {
		int key = v.getId();
		switch (key) {
		case R.id.Date:
			DatePickerDialog dateDialog = new DatePickerDialog(EditTodo.this,
					datePicker,
					// first time around show today's date
					myCalendar.get(Calendar.YEAR),
					myCalendar.get(Calendar.MONTH),
					myCalendar.get(Calendar.DAY_OF_MONTH));
			dateDialog.show();

			break;
		case R.id.Time:
			Calendar cal = Calendar.getInstance();
			cal.setTime(deadlineDate);
			int hour = cal.get(Calendar.HOUR_OF_DAY);
			int minute = cal.get(Calendar.MINUTE);
			TimePickerDialog dialog = new TimePickerDialog(this, timePicker,
					hour, minute, true);
			dialog.show();
			break;
		case R.id.btnSave:
			if(todo != null){//Update
				//Get Name
				todo.set_name(txtName.getText().toString());
				//Get deadline date
				todo.set_deadline_date(deadlineDate);
				todo.set_create_date(new Date());
				//Get Priority
				int iPriority = 1;
				if(rtbPriority.getRating() > 1)
					iPriority =(int) rtbPriority.getRating();
				todo.set_priority(iPriority);
				//Get category
				Category cat = (Category)spnCat.getSelectedItem();
				if (cat != null) {
					todo.set_catid(cat.get_id());
				} else {
					todo.set_catid(1);
				}
				//Get mail
				todo.set_email(txteMail.getText().toString());
				//Get Notes
				todo.set_notes(txtNote.getText().toString());
				
				todoDao = new TodoDAO(this);
				todoDao.UpdatePWD(todo);
			}
			else{ // Add
				todo = new ToDo();				
				//Get Name
				todo.set_name(txtName.getText().toString());
				//Get deadline date
				todo.set_deadline_date(deadlineDate);
				todo.set_create_date(new Date());
				//Get Priority
				int iPriority = 1;
				if(rtbPriority.getRating() > 1)
					iPriority =(int) rtbPriority.getRating();
				todo.set_priority(iPriority);
				//Get category
				Category cat = (Category)spnCat.getSelectedItem();
				if (cat != null) {
					todo.set_catid(cat.get_id());
				} else {
					todo.set_catid(1);
				}
				//Get mail
				todo.set_email(txteMail.getText().toString());
				//Get Notes
				todo.set_notes(txtNote.getText().toString());
				
				todoDao = new TodoDAO(this);
				todoDao.Add(todo);
			}			
			Intent inten = new Intent(EditTodo.this,MainActivity.class);
			startActivity(inten);
			EditTodo.this.finish();
			
			break;
		case R.id.btnDel:
			if(todo != null){
				todoDao = new TodoDAO(this);
				todoDao.deleteCat(todo.get_id());
				Intent intenDel = new Intent(EditTodo.this,MainActivity.class);
				startActivity(intenDel);
				EditTodo.this.finish();
			}
			break;
		case R.id.btn_todo_back:
			Intent intenDel = new Intent(EditTodo.this,MainActivity.class);
			startActivity(intenDel);
			EditTodo.this.finish();
			break;
		default:
			break;
		}

	}
}
